﻿Public Class FrmDetailsCom
    Dim a As Integer
    Dim b As Integer
    Private Sub FrmRechProd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            MySQLConnect.MonJeuDeDonnees.Tables("commande").Clear()
            MySQLConnect.MonJeuDeDonnees.Tables("produitscommander").Clear()
        Catch ex As Exception

        End Try
        a = ComboBox1.SelectedIndex + 1

        ComboBox1.DataSource = Executer_Requete_Select("", "commande")
        ComboBox1.ValueMember = "idCommande"
        ComboBox1.DisplayMember = "idCommande"

        TextBox1.Text = MySQLConnect.MonJeuDeDonnees.Tables("commande").Rows(a).Item(1)
        TextBox2.Text = MySQLConnect.MonJeuDeDonnees.Tables("commande").Rows(a).Item(2) & " " & MySQLConnect.MonJeuDeDonnees.Tables("commande").Rows(a).Item(3)

        ComboBoxProduit.DataSource = Executer_Requete_Select("SELECT * FROM produit", "produits")
        ComboBoxProduit.DisplayMember = "LibelleProduit"
        ComboBoxProduit.ValueMember = "idProduit"

        ComboBoxCategorie.DataSource = Executer_Requete_Select("SELECT * FROM categorie", "categorie")
        ComboBoxCategorie.DisplayMember = "LibelleCategorie"
        ComboBoxCategorie.ValueMember = "idCategorie"
        ComboBoxCategorie.SelectedValue = 0
    End Sub

    Private Sub btnAjout_Click(sender As Object, e As EventArgs) Handles btnAjout.Click
        If TextBoxQte.Text = "" Then
            ErrorProvider1.SetError(TextBoxQte, "Entrer une quantité !")
        Else
            ErrorProvider1.SetError(TextBoxQte, "")

            MySQLConnect.MaRequete = "INSERT INTO lignedecommande VALUES ('" & ComboBox1.SelectedIndex + 1 & "', '" & ComboBoxProduit.SelectedValue & "', '" & TextBoxQte.Text & "')"
            MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
            MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()

            Try
                MySQLConnect.MonJeuDeDonnees.Tables("produitscommander").Clear()
            Catch ex As Exception

            End Try
            MySQLConnect.MaRequete = "SELECT idCommande, LibelleProduit, QuantiteCom, PrixHTProduit, (PrixHTProduit * QuantiteCom) AS Total FROM lignedecommande, produit WHERE lignedecommande.idProduit = produit.idProduit AND idCommande = '" & ComboBox1.SelectedIndex + 1 & "'"
            MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
            MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialRequete
            MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "produitscommande")
            dgView.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("produitscommande")
        End If
    End Sub

    Private Sub btnFermer_Click(sender As Object, e As EventArgs) Handles btnFermer.Click
        Me.Close()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Try
            MySQLConnect.MonJeuDeDonnees.Tables("produitscommander").Clear()
        Catch ex As Exception

        End Try
        a = ComboBox1.SelectedIndex
        TextBox1.Text = MySQLConnect.MonJeuDeDonnees.Tables("commande").Rows(a).Item(1)
        TextBox2.Text = MySQLConnect.MonJeuDeDonnees.Tables("commande").Rows(a).Item(2) & " " & MySQLConnect.MonJeuDeDonnees.Tables("commande").Rows(a).Item(3)

        MySQLConnect.MaRequete = "SELECT idCommande, LibelleProduit, QuantiteCom, PrixHTProduit, (PrixHTProduit * QuantiteCom) AS Total FROM lignedecommande, produit WHERE lignedecommande.idProduit = produit.idProduit AND idCommande = '" & ComboBox1.SelectedIndex + 1 & "'"
        MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
        MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialRequete
        MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "produitscommander")
        dgView.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("produitscommander")
    End Sub

    Private Sub ComboBoxCategorie_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxCategorie.SelectedIndexChanged
        Try
            MySQLConnect.MonJeuDeDonnees.Tables("catprodselec").Clear()
            b = ComboBoxCategorie.SelectedValue
        Catch ex As Exception

        End Try
        MySQLConnect.MaRequete = "SELECT * FROM produit WHERE idCat = " & b & ""
        MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
        MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialRequete
        MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "catprodselec")
        ComboBoxProduit.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("catprodselec")
    End Sub

    Private Sub btnSuppr_Click(sender As Object, e As EventArgs) Handles btnSuppr.Click

    End Sub

    Private Sub btnPDF_Click(sender As Object, e As EventArgs) Handles btnPDF.Click
        genererpdfpuisdatatable("Liste des Produits", dgView.DataSource, "listeproduits")
    End Sub
End Class